package com.example.demo.controller.front;

import com.example.demo.dao.*;
import com.example.demo.mapper.OrderitemMapper;
import com.example.demo.service.*;
import com.example.demo.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/indent")
public class IndentC {
   @Autowired
   private indentService indentService;
   @Autowired
   private AddressService addressService;

   @Autowired
   private GoodsService goodsService;
   @Autowired
   private OrderItemService orderItemService;
    @PostMapping(value = "/add")
    @ResponseBody
    public Msg addIndent(Goods goods, HttpSession session){
        //时间
        Date time = new Date();
        java.sql.Date date = new java.sql.Date(time.getTime());
        Integer userid =(Integer) session.getAttribute("id");
        Integer goodsid = goods.getGoodsid();
        Integer num = goods.getNum();
        Address address =addressService.findAddressByuserId(userid);
        String msg="";
        if(address!=null){
            Integer addressid = address.getAddressid();
            Goods goods1 = goodsService.findGoodsByPrimaryKey(goodsid);
            float oldprice =goods1.getPrice();
            float newprice= goods1.getPrice();
            Indent indent = new Indent(1,userid,date,oldprice,newprice,(byte)0,(byte)0,(byte)0,(byte)0,addressid);
            indentService.addIndent(indent);
            Integer orderid = indent.getOrderid();
            Orderitem orderitem = new Orderitem(1,orderid,goodsid,num);
            orderItemService.addIndentGoods(orderitem);
            return Msg.success("添加成功");
        }else{
            return Msg.fail("请先选择送货地址");
        }
    }
}
